const Sequelize = require('sequelize');
const sequelize = require('../libs/sequelize')

const Upload = require('../libs/upload')


const Artist = require('./artist')
const Album = sequelize.define('album', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    name: {
        type: Sequelize.STRING
    },
    poster: {
        type: Sequelize.STRING,
    },
    publishDate: {
        type: Sequelize.DATE,
    },
    playCount: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    },
    artistId: {
        type: Sequelize.INTEGER
    }
});

// Album.sync({ force: true }).then(() => {

//     console.log('done ')

// });


Album.belongsTo(Artist);


Album.handleFile = function(list) {
    const new_list = list.map(item => {
        const temp = {}
        Object.assign(temp, item)
            // lodash
        temp.poster = Upload.getUrl(temp.poster)
        return temp
    })
    return new_list
}


module.exports = Album